package com.alstudio.utils.android;

import android.content.Context;
import android.telephony.CellLocation;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.telephony.cdma.CdmaCellLocation;
import android.telephony.gsm.GsmCellLocation;

import com.alstudio.utils.android.geolocation.ALLocationListener;
import com.alstudio.utils.android.geolocation.ALGeoLocationData;
import com.alstudio.utils.log.Mylog;

public class ALTelePhoneManager {

	private static AlPhoneLocationListener mLocationListener;
	private static Context locateContext;

	public static String getImei(Context ctx) {

		String imei = null;
		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			imei = mTelephonyManager.getDeviceId();
			Mylog.d("get imei success: " + imei);
		} else {
			Mylog.e("get imei failed: no TelephonyManager");
		}
		return imei;
	}

	public static int getPhoneType(Context ctx) {
		int type = TelephonyManager.PHONE_TYPE_NONE;
		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			type = mTelephonyManager.getPhoneType();
			Mylog.e("get phone type success: " + type);
		} else {
			Mylog.e("get phone type failed: no TelephonyManager");
		}
		return type;
	}

	public static int getNetworkType(Context ctx) {
		int type = TelephonyManager.NETWORK_TYPE_UNKNOWN;
		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			type = mTelephonyManager.getNetworkType();
			Mylog.e("get network type success: " + type);
		} else {
			Mylog.e("get network type failed: no TelephonyManager");
		}
		return type;
	}

	public static int getNetworkDataState(Context ctx) {
		int state = -1;
		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			state = mTelephonyManager.getDataState();
			Mylog.e("get network data state success: " + state);
		} else {
			Mylog.e("get network type failed: no TelephonyManager");
		}
		return state;
	}

	public static String getCellId(Context ctx) {
		String cellid = null;
		if (getPhoneType(ctx) == TelephonyManager.PHONE_TYPE_GSM) {
			TelephonyManager mTelephonyManager = AndroidUtils
					.getTelephonyManager(ctx);
			if (mTelephonyManager != null) {
				GsmCellLocation location = (GsmCellLocation) mTelephonyManager
						.getCellLocation();
				if (location != null) {
					cellid = location.getCid() + "";
				}
			}
		}
		return cellid;
	}

	public static String getLac(Context ctx) {
		String lac = null;
		if (getPhoneType(ctx) == TelephonyManager.PHONE_TYPE_GSM) {
			TelephonyManager mTelephonyManager = AndroidUtils
					.getTelephonyManager(ctx);
			if (mTelephonyManager != null) {
				GsmCellLocation location = (GsmCellLocation) mTelephonyManager
						.getCellLocation();
				if (location != null) {
					lac = location.getLac() + "";
				}
			}
		}
		return lac;
	}

	public static String getCdmaLat(Context ctx) {
		String lat = null;
		if (getPhoneType(ctx) == TelephonyManager.PHONE_TYPE_GSM) {
			TelephonyManager mTelephonyManager = AndroidUtils
					.getTelephonyManager(ctx);
			if (mTelephonyManager != null) {
				CdmaCellLocation location = (CdmaCellLocation) mTelephonyManager
						.getCellLocation();
				if (location != null) {
					lat = location.getBaseStationLatitude() + "";
				}
			}

		}
		return lat;
	}

	public static String getCdmaLng(Context ctx) {
		String lng = null;
		if (getPhoneType(ctx) == TelephonyManager.PHONE_TYPE_GSM) {
			TelephonyManager mTelephonyManager = AndroidUtils
					.getTelephonyManager(ctx);
			if (mTelephonyManager != null) {
				CdmaCellLocation location = (CdmaCellLocation) mTelephonyManager
						.getCellLocation();
				if (location != null) {
					lng = location.getBaseStationLongitude() + "";
				}
			}

		}
		return lng;
	}

	public static void addPhoneListener(Context ctx,
			PhoneStateListener listener, int events) {

		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			mTelephonyManager.listen(listener, events);
		} else {
			Mylog.e("set phone listener failed: no TelephonyManager");
		}
	}

	public static void removePhoneListener(Context ctx, int events) {

		TelephonyManager mTelephonyManager = AndroidUtils
				.getTelephonyManager(ctx);
		if (mTelephonyManager != null) {
			mTelephonyManager.listen(null, events);
			Mylog.e("remove phone listener success");
		} else {
			Mylog.e("remove phone listener failed: no TelephonyManager");
		}
	}

	public static void startPhoneLocateModule(Context context,
			AlPhoneLocationListener listener) {
		addPhoneListener(context, phoneLocateListener,
				PhoneStateListener.LISTEN_CELL_LOCATION);
		mLocationListener = listener;
		locateContext = context;
	}

	public static void stopPhoneLocateModule(Context context) {
		removePhoneListener(context, PhoneStateListener.LISTEN_CELL_LOCATION);
	}

	private static PhoneStateListener phoneLocateListener = new PhoneStateListener() {

		@Override
		public void onCellLocationChanged(CellLocation location) {
			// TODO Auto-generated method stub
			super.onCellLocationChanged(location);
			notifyLocationChanged(location);
		}
	};

	private static void notifyLocationChanged(CellLocation location) {

		Mylog.d("phone location changed!");

		if (locateContext != null) {
			String cellid = null;
			String lac = null;
			String cdmaLat = null;
			String cdmaLng = null;

			if (getPhoneType(locateContext) == TelephonyManager.PHONE_TYPE_GSM) {
				try {
					GsmCellLocation mLocation = (GsmCellLocation) location;

					cellid = "" + mLocation.getCid();
					lac = "" + mLocation.getLac();

				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			} else {
				// cdma定位只能上传转换后的gps信息
				try {
					CdmaCellLocation mLocation = (CdmaCellLocation) location;

					cdmaLat = "" + mLocation.getBaseStationLatitude();
					cdmaLng = "" + mLocation.getBaseStationLongitude();
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (mLocationListener != null) {
				ALGeoLocationData data = new ALGeoLocationData();
				data.setCdmaLat(cdmaLat);
				data.setCdmaLng(cdmaLng);
				data.setCellid(cellid);
				data.setLac(lac);
				mLocationListener.onLocationChanged(data);
			}

		}

	}

	public interface AlPhoneLocationListener extends ALLocationListener {
	}

}
